package com.utils;


import com.translate.StringUtil;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class TeansUtils {


    public static void main(String[] args) throws Exception {
//        String path="C:\\Users\\Administrator.DESKTOP-GTF5LQT\\Desktop\\临时文件\\呼叫中心\\PRD Set up Automatic Process to transfer data to DELL GC Commercial 1.3.xlsx";
        String path="C:\\Users\\Administrator.DESKTOP-GTF5LQT\\Desktop\\临时文件\\呼叫中心\\ATLAS视图表结构和样例-v0.3.xlsx";
        String dburl="jdbc:mysql://localhost:3228/ATLAS?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true";
        String dbUserName="root";
        String psd="Sinobase@123";

        List<Map<String, Object>> excleInfo = ATLASUtils.importExcel(path);
        List<String> tablesNames = MysqlDbUtil.showTables(dburl, dbUserName, psd);

        excleInfo.forEach(map->{
            if(map!=null) {
                String tableName = map.get("tableName").toString();
                List<FRDEntity> columInfo = (List<FRDEntity>) map.get("colum");
                if (!tablesNames.contains(tableName)) {
                    StringBuilder sql = new StringBuilder("CREATE TABLE `").append(tableName).append("`( ");
                    AtomicInteger maxColumNum=new AtomicInteger(0);
                    columInfo.forEach(frdEntity -> {
                        final String type = getType(frdEntity.getFieldType());
                        sql.append("`").append(frdEntity.getFieldName().trim()).append("` ")
                                .append(type);

                        if(!"date".equals(type)&&!"timestamp".equals(type)&&!"datetime".equals(type)) {
                            if("int".equals(type)&&frdEntity.getFieldLength().equals("0")){

                            }else {
                                sql.append(" (").append(frdEntity.getFieldLength()).append(") ");

                            }
                        }

//                        else if("bigint".equals(type)||"bignit".equals(type)){
//                            sql.append(" (").append(20).append(") ");
//                        }
                        if(!"timestamp".equals(type)) {
                            if (frdEntity.getIsRequired()) {
                                sql.append(" NOT NULL ");
                            } else {
                                sql.append(" DEFAULT NULL ");
                            }
                        }else{
                            sql.append(" NOT NULL DEFAULT CURRENT_TIMESTAMP ");
                        }
                        sql.append(" COMMENT ").append("\"").append(frdEntity.getDes().trim()).append("\"");
                        sql.append(",");
                    });
                    StringBuilder stringBuilder = sql.deleteCharAt(sql.lastIndexOf(","));
                    stringBuilder.append(")");
                    MysqlDbUtil.excute(stringBuilder.toString(), dburl, dbUserName, psd);
                }
            }
        });

    }

    private  static String getType(String tableType) {

        switch (tableType) {
            case "nvarchar":
            case "NVARCHAR":
            case "char":
            case "VARCHAR":
                return "varchar";
            case "TIMESTAMP":
                return "timestamp";
            case "datetime2":
                return "datetime";
            case "BIGINT":
            case "BIGNIT":
                return "bigint";
            case "INTEGAR":
            case "INETEGER":
            case "numeric":
                return "int";
            default:
                return StringUtils.lowerCase(tableType);

        }
    }
}
